﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FamilyTree
{
    public partial class Treeupdate : Form
    {
        public Treeupdate()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source = PC-20190806MQPW\SQLEXPRESS01; Initial Catalog =终极电子族谱; User ID = sa; Password = 123456";
            string id = textBox1.Text;
            SqlConnection conn = new SqlConnection(connString);

            conn.Open();
            string sql = @"SELECT * FROM tree WHERE id='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (!dr.Read())
            {
                MessageBox.Show(id + "族谱不存在！");
                return;
            }
            if (comboBox1.Text.Equals("族谱名"))
            {
                yuan.Text = dr["name"].ToString();
            }
            else if (comboBox1.Text.Equals("家族事件"))
            {
                yuan.Text = dr["brief"].ToString();
            }
            else if (comboBox1.Text.Equals("家训"))
            {
                yuan.Text = dr["ruler"].ToString();
            }
            
            conn.Close();
        }

        private void label5_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source = PC-20190806MQPW\SQLEXPRESS01; Initial Catalog = 终极电子族谱; User ID = sa; Password = 123456";
            string id = textBox1.Text;
            string get = comboBox1.Text;
            string sql;
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            if (get.Equals("族谱名"))
            {
                sql = @"UPDATE tree SET [name]='" + xiugaiwei.Text + "'";
                sql += " WHERE [id]='" + textBox1.Text + "'";

                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show(textBox1.Text + "号族谱修改成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("更新出错！" + ex.Message);
                }
                conn.Close();
            }
            else if (get.Equals("家族事件"))
            {
                sql = @"UPDATE tree SET [brief]='" + xiugaiwei.Text + "'";
                sql += " WHERE [id]='" + textBox1.Text + "'";

                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show(textBox1.Text + "号族谱修改成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("更新出错！" + ex.Message);
                }
                conn.Close();
            }
            else if (get.Equals("家训"))
            {
                sql = @"UPDATE tree SET [ruler]='" + xiugaiwei.Text + "'";
                sql += " WHERE [id]='" + textBox1.Text + "'";

                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show(textBox1.Text + "号族谱修改成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("更新出错！" + ex.Message);
                }
                conn.Close();
            }
        }

        private void Treeupdate_Load(object sender, EventArgs e)
        {

        }
    }
}
